package uk.ac.lkl.lpp.ui;



import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.Toolkit;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;


/**
 * For now, is a JDialog. Needn't be.
 * 
 * 
 * 
 * @author Darren Pearce
 * @version $Revision$
 * @version $Date$
 * 
 */
public class HyperlinkEditor extends JDialog {

    private JTextField linkField;

    private ArrayList<ActionListener> listeners = new ArrayList<ActionListener>();

    private JButton okButton;


    public HyperlinkEditor() {
        super();
        setLayout(new BorderLayout());
        setUndecorated(true);
        linkField = new JTextField(20);

        JPanel editorPanel = new JPanel(new BorderLayout());

        editorPanel.setBorder(BorderFactory.createEtchedBorder());

        editorPanel.add(linkField, BorderLayout.CENTER);
        okButton = new JButton("Ok");
        editorPanel.add(okButton, BorderLayout.EAST);
        okButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                HyperlinkEditor.this.setVisible(false);
                fireActionPerformed(e);
            }
        });

        add(editorPanel);
        pack();
    }


    public String getLinkText() {
        return linkField.getText();
    }


    public void setLinkText(String linkText) {
        linkField.setText(linkText);
        
    }


    public void addActionListener(ActionListener listener) {
        listeners.add(listener);
    }


    public void removeActionListener(ActionListener listener) {
        listeners.remove(listener);
    }


    private void fireActionPerformed(ActionEvent e) {
        for (ActionListener listener : listeners)
            listener.actionPerformed(e);
    }


    /**
     * Show relative to the given component.
     * 
     * @param component
     * 
     */
    public void show(Component component) {

        // to ensure that if open then is closed appropriately
        if (isVisible())
            okButton.doClick();

        Point componentLocation = component.getLocation();
        SwingUtilities.convertPointToScreen(componentLocation, component
                .getParent());

        int editorLocationX = componentLocation.x + component.getWidth();
        int editorLocationY = componentLocation.y;

        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

        if (editorLocationX + getWidth() > screenSize.width)
            editorLocationX = screenSize.width - getWidth();

        setLocation(editorLocationX, editorLocationY);

        setVisible(true);
    }
}
